/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

/**
 *
 * @author David Harrison
 * 3 May 2008
 *
 */

import java.io.IOException;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.DontValidate;

import com.sfs.postcodes.dao.PostcodeLookupException;
import com.sfs.postcodes.dao.GeocodeLookupException;


/**
 * The Class LookupInternationalActionBean.
 */
@UrlBinding("/web/lookup/international")
public class LookupInternationalActionBean extends LookupBaseActionBean {

    /** The postcode JSP. */
    private static final String POSTCODE = "lookup-international.jsp";


    /**
     * The Welcome action.
     *
     * @return the resolution
     */
    @DefaultHandler
    @DontValidate
    public final Resolution welcome() {
        Resolution response = new ForwardResolution(getView(POSTCODE));

        return response;
    }

    /**
     * The search action.
     *
     * @return the resolution
     */
    public final Resolution search() {

        Resolution response = new ForwardResolution(getView(POSTCODE));

        try {
            setPostcode(getPostcodeDAO().findPostcode(getAddress(),
                    getState(), getCountry()));
        } catch (PostcodeLookupException pe) {
            this.addMessage("Error performing postcode lookup: "
                    + pe.getMessage());
        }

        try {
            setGeocode(getGeocoderDAO().findGeocode(getAddress(),
                    getState(), getCountry()));
        } catch (GeocodeLookupException ge) {
            this.addMessage("Error performing geocode lookup: "
                    + ge.getMessage());
        } catch (IOException io) {
            this.addMessage("Error preparing for geocode lookup: "
                    + io.getMessage());
        }
        return response;
    }
}
